#include"../Include/all.h"
extern size_t get_filesize(int fd);
extern int is_dir(char* path);
extern int endwith(char* s,char c);
extern char* str_contact(char* str1,char* str2);
extern void *thread_copy_fn(void *arg);
extern void Copy(char *spathname,char *tpathname);
extern void copy_folder(char* source_path,char *destination_path);
extern int remove_dir(const char *dir);
extern void trave_dir(char* path,char* path2);
extern void trave_dira(char* path,char* path2);
extern void trave_diraz(char* path,char* path2);
extern void ProgressBar();
int main()
{
    struct stat buf;
    char path_A[1000],path_B[1000];
    char temp1[1000],temp2[1000];
    DIR *dir_A,*dir_B;
    printf("请输入A的路径:");
    scanf("%s",path_A);
    dir_A=opendir(path_A);
    if(dir_A==NULL)
    {
        printf("This diretory don't exist!\n");
        return 0;
        }
    if(stat(path_A,&buf)!=0)
    {
        printf("ERROR!\n");
        return 0;
        }
    printf("请输入B的路径:");
    scanf("%s",path_B);
    dir_B=opendir(path_B);
    if(dir_B==NULL)
    {
        mkdir(path_B,buf.st_mode);
        chown(path_B,buf.st_uid,buf.st_gid);
        dir_B=opendir(path_B);
        }
    else
    {
        chmod(path_B,buf.st_mode);
        chown(path_B,buf.st_uid,buf.st_gid);
        }
    int choose;
    printf("***************************\n");
    printf("******欢迎使用本系统*******\n");
    printf("******1.把A备份到B*********\n");
    printf("******2.把B同步到A*********\n");
    printf("******3.把A同步到B*********\n");
    printf("******4.A,B同时同步********\n");
    printf("******5.退出本系统*********\n");
    printf("***************************\n");
    printf("请选择\n");
    scanf("%d",&choose);
    if(choose==1){
        //把A备份到B
        printf("开始备份..........\n");
        strcpy(temp1,path_A);
        strcpy(temp2,path_B);
        trave_dira(temp1,temp2);
        trave_diraz(temp2,temp1);
        ProgressBar();
        printf("备份完成\n");
        }
    if(choose==2){
        //把B同步到A
        printf("开始同步..........\n");
        trave_dira(path_B,path_A);
        trave_diraz(path_A,path_B);
        ProgressBar();
        printf("同步完成\n");
        }
    if(choose==3){
        //把A同步到B
        printf("开始同步..........\n");
        trave_dira(path_A,path_B);
        trave_diraz(path_B,path_A);
        ProgressBar();
        printf("同步完成\n");
        }
    if(choose==4){
        //把A,B同时同步
        printf("开始同步..........\n");
        trave_dir(path_A,path_B);
        trave_dir(path_B,path_A);
        ProgressBar();
        printf("同步完成\n");
        
        }
    if(choose==5){
        //退出
        closedir(dir_A);
        closedir(dir_B);
        printf(" \n\n \n\n");
	    printf("*********************************");
	    printf("\n\n        欢迎下次使用           \n\n");
	    printf("*********************************\n\n");
        exit(1);
        }
    return 0;

}
